<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="货物名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入货物名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新日期" prop="changgeTime">
        <el-date-picker clearable
          v-model="queryParams.changgeTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择日期">
        </el-date-picker>
      </el-form-item>
      <br>
      <el-form-item label="货物状态" prop="goodstate">
        <el-select v-model="queryParams.goodstate" placeholder="请选择货物状态" clearable @change="handleQuery" @keyup.enter.native="handleQuery">
          <el-option label="缺货" value="0"></el-option>
          <el-option label="充足" value="1"></el-option>
          <!-- 在这里添加更多的选项 -->
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:warehouse:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:warehouse:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:warehouse:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:warehouse:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warehouseList" @selection-change="handleSelectionChange" stripe>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="货物序号" align="center" prop="goodsId" />
      <el-table-column label="货物名称" align="center" prop="goodsName" />
      <el-table-column label="更新日期" align="center" prop="changgeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.changgeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="货物状态" align="center" prop="goodstate">
        <template slot-scope="scope">
          <span v-if="scope.row.goodstate === '0'">缺货</span>
          <span v-else-if="scope.row.goodstate === '1'">充足</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:warehouse:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:warehouse:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="货物名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入货物名称" />
        </el-form-item>
        <el-form-item label="归属店铺" prop="storeName">
          <el-select v-model="form.storeId" filterable placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.storeId"
              :label="item.storeName"
              :value="item.storeId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分配店铺" >
          <el-input v-model="allocatedStore" readonly size="small"></el-input>
        </el-form-item>
        <el-form-item label="更新日期" prop="changgeTime">
          <el-date-picker clearable
            v-model="form.changgeTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="货物状态" prop="goodstate">
          <el-select v-model="form.goodstate" placeholder="请选择货物状态">
            <el-option label="缺货" value="0"></el-option>
            <el-option label="充足" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-col :span="10">
      <el-card shadow="hover" style="width: 100%;">
        <div slot="header" class="clearfix">
          <span>友情提醒</span>
          <el-button
            style="float: right; padding: 3px 0"
            type="text"
            @click="clickFn"
          >暂停</el-button
          >
        </div>
        <el-statistic
          ref="statistic"
          @finish="hilarity"
          format="HH:mm:ss"
          :value="deadline4"
          title="距离明日："
          time-indices
        >
        </el-statistic>
      </el-card>
    </el-col>
    <br><br><br><br><br><br><p>若您空闲请给一个五星好评哦！！！！</p>
    <div class="block" >
      <span class="demonstration">功能评价</span>
      <el-rate
        v-model="value1"
        show-text>
      </el-rate>
    </div>
    <div class="block">
      <span class="demonstration">页面评价</span>
      <el-rate
        v-model="value2"
        show-text>
      </el-rate>
    </div>
  </div>
</template>

<script>
import { assignGoodsToWarehouse,listWarehouse, getWarehouse, delWarehouse, addWarehouse, updateWarehouse } from "@/api/system/warehouse";

export default {
  name: "Warehouse",
  data() {
    return {
      deadline2: Date.now() + 1000 * 60 * 60 * 8,
      deadline3: Date.now() + 1000 * 60 * 30,
      deadline4: Date.now() + (new Date().setHours(23, 59, 59) - Date.now()),
      deadline5: new Date("2024-05-10"),
      allocatedStore: '',
      // 保存已分配的店铺名称
      options: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库信息管理表格数据
      warehouseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: null,
        changgeTime: null,
        goodstate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsName: [
          { required: true, message: "货物名称不能为空", trigger: "blur" }
        ],
        changgeTime: [
          { required: true, message: "更新日期不能为空", trigger: "blur" }
        ],
        goodstate: [
          { required: true, message: "货物状态不能为空", trigger: "blur" }
        ]
      },
      value1: null,
      value2: null,
      colors: ['#99A9BF', '#F7BA2A', '#FF9900']  // 等同于 { 2: '#99A9BF', 4: { value: '#F7BA2A', excluded: true }, 5: '#FF9900' }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询仓库信息管理列表 */
    getList() {
      this.loading = true;
      listWarehouse(this.queryParams).then(response => {
        this.warehouseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        goodsId: null,
        goodsName: null,
        changgeTime: null,
        goodstate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加仓库信息管理";
      assignGoodsToWarehouse().then(aaa =>{
        this.options =aaa.data
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const goodsId = row.goodsId || this.ids
      getWarehouse(goodsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓库信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.goodsId != null) {
            updateWarehouse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              // 更新已分配店铺名称
              this.allocatedStore = this.options.find(option => option.storeId === this.form.storeId).storeName;
            });
          } else {
            addWarehouse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              // 更新已分配店铺名称
              this.allocatedStore = this.options.find(option => option.storeId === this.form.storeId).storeName;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const goodsIds = row.goodsId || this.ids;
      this.$modal.confirm('是否确认删除仓库信息管理编号为"' + goodsIds + '"的数据项？').then(function() {
        return delWarehouse(goodsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/warehouse/export', {
        ...this.queryParams
      }, `warehouse_${new Date().getTime()}.xlsx`)
    },
    hilarity() {
      this.$notify({
        title: "提示",
        message: "时间已到",
        duration: 0,
      });
    },
    clickFn() {
      this.$refs.statistic.suspend(this.stop);
      this.stop = !this.stop;
    },
    add() {
      this.deadline3 = this.deadline3 + 1000 * 10;
    },
  }
};
</script>
